import { login } from '../services/login';
// import { routerRedux } from 'dva/router';
import { isSuccess } from '../configs/error';

export default {
	namespace: 'login',
	state: {
		loginLoading: false,
	},

	effects: {
		*login({ payload }, { put, call }) {
			yield put({ type: 'showLoginLoading' });
			const data = yield call(login, payload);
			if (isSuccess(data)) {
				const dataObject = data.data;
				window.localStorage.setItem('access_token', dataObject.token);
				yield put({ type: 'app/init', payload: dataObject, isLogin: true });// 初始化用户必要数据
			} else {
				yield put({ type: 'hideLoginLoading' });
			}
		},
	},
	reducers: {
		showLoginLoading(state) {
			return {
				...state,
				loginLoading: true,
			};
		},
		hideLoginLoading(state) {
			return {
				...state,
				loginLoading: false,
			};
		},
	},
};

